#!/bin/env python
# -*- coding:utf-8
"""
@author:xingz
@file:demo2.py
@time:2021/11/27
"""
from typing import *
import pandas as pd


class D(Dict):
    def __init__(self, data: Dict, df):
        super(D, self).__init__()
        self.data: Dict = data
        self.df = df

    def __getitem__(self, item):
        return self.data[item]

    def __setitem__(self, key, value):
        self.data[key] = value

    def __iter__(self):
        return self.data


def get_df():
    with open("regressionresult.csv", "r") as f:
        result_csv = f.readlines()

    result_pd = pd.read_csv("regressionresult.csv", header=None)
    df = pd.DataFrame(result_pd)
    # df[5] = df[5].astype(int)
    df_DRC = df[df[3] == "DRC"]
    df_LVS = df[df[3] == "LVS"]
    df_ERC = df[df[3] == "ERC"]
    df_SOFTCK = df[df[3] == "SOFTCK"]
    return df_DRC, df_LVS, df_ERC, df_SOFTCK


def analyse():
    df_DRC, df_LVS, df_ERC, df_SOFTCK = get_df()
    drc_dict = {}
    drc_error_set = set([i[4] for i in df_DRC.values])
    for i in drc_error_set:
        drc_dict[i] = {}
        drc_dict[i]["count"] = sum(list(df_DRC[df_DRC[4] == i][5]))
        drc_dict[i]["comp"] = list(df_DRC[df_DRC[4] == i][2])

    lvs_dict = {}
    lvs_error_set = set([i[4] for i in df_LVS.values])
    for i in lvs_error_set:
        lvs_dict[i] = {}
        lvs_dict[i]["count"] = sum(list(df_LVS[df_LVS[4] == i][5]))
        lvs_dict[i]["comp"] = list(df_LVS[df_LVS[4] == i][2])

    erc_dict = {}
    erc_error_set = set([i[4] for i in df_ERC.values])
    for i in erc_error_set:
        erc_dict[i] = {}
        erc_dict[i]["count"] = sum(list(df_ERC[df_ERC[4] == i][5]))
        erc_dict[i]["comp"] = list(df_ERC[df_ERC[4] == i][2])

    sft_dict = {}
    sft_error_set = set([i[4] for i in df_SOFTCK.values])
    for i in sft_error_set:
        sft_dict[i] = {}
        sft_dict[i]["count"] = sum(list(df_SOFTCK[df_SOFTCK[4] == i][5]))
        sft_dict[i]["comp"] = list(df_SOFTCK[df_SOFTCK[4] == i][2])

    return drc_dict, lvs_dict, erc_dict, sft_dict
    # return D(drc_dict, df_DRC), D(lvs_dict, df_LVS), D(erc_dict, df_ERC), D(sft_dict, df_SOFTCK)


if __name__ == '__main__':
    drc_dict, lvs_dict, erc_dict, sft_dict = analyse()
    # print({k: v for k, v in drc_dict.items()})
    print(drc_dict)
